package conversion.mapper;

import conversion.bean.User;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface UserMapper {
    @Select("SELECT * FROM users")
    List<User> getAllUsers();

    @Select("SELECT username FROM users")
    List<String> getAllUsernames();

    @Select("SELECT * FROM users WHERE uuid = #{UUID}")
    User getUserByUUID(String uuid);

    @Delete("DELETE FROM Users WHERE uuid = #{UUID}")
    void deleteUserByUUID(String uuid);

    @Select("SELECT * FROM users WHERE username = #{username}")
    User getUserByUsername(String username);

    @Update("<script>" +
        "UPDATE users" +
        "<set>" +
        "<if test='username != null'>username = #{username},</if>" +
        "<if test='password != null'>password = #{password},</if>" +
        "<if test='email != null'>email = #{email},</if>" +
        "<if test='avatar != null'>avatar = #{avatar},</if>" +
        "<if test='gender != null'>gender = #{gender},</if>" +
        "<if test='phone != null'>phone = #{phone},</if>" +
        "<if test='address != null'>address = #{address},</if>" +
        "<if test='role != null'>role = #{role},</if>" +
        "<if test='enabled != null'>enabled = #{enabled},</if>" +
        "<if test='accountNoExpired != null'>account_no_expired = #{accountNoExpired},</if>" +
        "<if test='credentialsNoExpired != null'>credentials_no_expired = #{credentialsNoExpired},</if>" +
        "<if test='accountNoLocked != null'>account_no_locked = #{accountNoLocked},</if>" +
        "</set>" +
        "WHERE uuid = #{UUID}" +
        "</script>")
    void updateUserByUUID(User user);

    @Insert("INSERT INTO users (uuid, username, password, email, avatar, gender, phone, address, role, enabled, account_no_expired, credentials_no_expired, account_no_locked) " +
            "VALUES (#{UUID}, #{username}, #{password}, #{email}, #{avatar}, #{gender}, #{phone}, #{address}, #{role}, #{enabled}, #{accountNoExpired}, #{credentialsNoExpired}, #{accountNoLocked})")
    void addUser(User user);

    @Select("SELECT * FROM users WHERE email = #{email}")
    User findUserByEmail(@Param("email") String email);

}
